package lightswitch.server.code.sqlhelper.statement.insert;

import java.util.ArrayList;
import java.util.List;

import lightswitch.server.code.object.MyEntry;
import lightswitch.server.code.util.StringHelper;
import lightswitch.server.code.util.TypeOfData;

public class InsertItem {
	
	List<MyEntry> entryList;

	public InsertItem(String statement,List<String> columnList ) {
		entryList =new ArrayList<>();
		statement = StringHelper.NormalizeString(statement);
		if (!statement.equals("")) {
			statement = statement.substring(1, statement.length() - 1);

			String[] str = statement.split(",");
			for (int i = 0; i < str.length; i++) {
				String entryName = columnList.size() > 0 ? statement + "."
						+ columnList.get(i) : "";
				str[i] = StringHelper.NormalizeString(str[i]);
				MyEntry en = null;
				if (str[i].equals("true")) {
					en = new MyEntry(entryName, "true", TypeOfData.BOOLEAN);

				} else

				if (str[i].equals("false")) {
					en = new MyEntry(entryName, "false", TypeOfData.BOOLEAN);

				} else if (str[i].equals("null")) {
					en = new MyEntry(entryName, "null", TypeOfData.CHAR);

				} else

				if (str[i].matches("'[^']*'")) {
					en = new MyEntry(entryName, str[i].substring(1,
							str[i].length() - 1), TypeOfData.CHAR);
				} else {
					if (str[i].matches("[0-9]+")) {
						en = new MyEntry(entryName, str[i], TypeOfData.INT);

					} else {
						if (str[i].matches("[0-9.]+")
								&& str[i].split("\\.").length <= 2)// 1 dau phay
																	// hoac ko
																	// co
						{
							en = new MyEntry(entryName, str[i],
									TypeOfData.FLOAT);
						} else {
							// throw exception
							return;
						}
					}
				}

				entryList.add(en);
			}
		}

	}
}
